﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.Web.Security;

namespace Sc.Web.Controllers
{
    public class BaseController : Controller
    {
        private UserLogin userLogin = null;
        /// <summary>
        /// 登陆用户信息
        /// </summary>
        protected UserLogin UserLogin
        {
            get
            {
                if (User.Identity.IsAuthenticated)
                {
                    if (userLogin == null)
                    {
                        FormsIdentity id = (FormsIdentity)User.Identity;
                        FormsAuthenticationTicket ticket = id.Ticket;
                        var data = ticket.UserData;
                        JavaScriptSerializer json = new JavaScriptSerializer();
                        var u = json.Deserialize<UserLogin>(data);
                        userLogin = u;
                    }
                    return userLogin;
                }
                return null;
            }
        }
        /// <summary>
        /// 执行Action前检查
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //UserHelper.ValidateUserFeatureAuthority(filterContext);
            base.OnActionExecuting(filterContext);
        }
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <returns></returns>
        public string FileUpload()
        {
            var file = Request.Files[0];
            var path = Guid.NewGuid() + file.FileName;
            if (!Directory.Exists(Server.MapPath("~/UploadFiles/CarImg")))
            {
                Directory.CreateDirectory(Server.MapPath("~/UploadFiles/CarImg"));
            }
            var filePath = Server.MapPath("~/UploadFiles/CarImg/" + path);
            file.SaveAs(filePath);//上传文件
            return path;
        }
	}
}